/**
 * 无限级别分类(SELECT标签版)
 * @param data 树形分类数据
 * @param cont 容器ID
 * @param defaultOption 默认option
 * @date 2018 04 27
 * @author YUE
 */
function NoLimitCategory(data, cont, defaultOption){
	//参数检查
	if(typeof(arguments[0]) != 'object'){alert("无限极分类：缺少数据参数");return};
	if(typeof(arguments[1]) != 'object'){alert("无限极分类：缺少容器对象");return};
	if(!arguments[2])defaultOption = "";
	
	if(!(data.length > 0)){
		alert("无限极分类：数据不完整");return
	}
	
	var self = this;
	
	this.onSelectCreate = function(obj, n){};
	
	this.append = function(cids){
		appendSelect(data, cids);	
	}
	
	this.getSelects = function(){
		return cont.getElementsByTagName("select");	
	}

	/**
	 * 追加SELECT标签
	 * @param data 树形数据，包括当前一级和所有子分类，在递归的时候此参数传子一级的数据
	 * @param cids 默认选择的分类ID数组，按照递归逐一分配，在初始化默认选项的时候会用到
	 */
	var appendSelect = function(data, cids){
		if(!arguments[1])cids = [];

		var select = document.createElement("select");
		cont.appendChild(select);
		self.onSelectCreate(select, self.getSelects().length);
		
		if(data.length > 0 && typeof(defaultOption) == 'string' && defaultOption != ''){
			var option = document.createElement("option");
			option.value = 0;
			option.innerHTML = defaultOption;
			select.appendChild(option);
		}
		
		var selected_cid = (cids.length>0 && cids[0]>0) ? cids[0] : 0;

		for(var i=0; i<data.length; i++){
			var selectedoption = 0;
			var option = document.createElement("option");
			option.value = data[i].id;
			option.innerHTML = data[i].name;
			
			if(selected_cid > 0 && cids[0] == data[i].id) {
				option.selected = "selected";
				selectedoption = i;
			}
			select.appendChild(option);
			
			if(i == selectedoption){
				var childrenData = (typeof(data[i].children) == 'undefined') ? null : data[i].children;
			}

		}
		
		//追加下一级
		if(childrenData != null && childrenData.length > 0){
			if(cids.length > 1){
				cids.splice(0,1);
			}
			if(cids.length > 0){
				appendSelect(childrenData, cids);
			}else{
				appendSelect(childrenData);	
			}
		}
		
		//为SELECT标签添加事件
		select.onchange = function(){
			removeSelectAfter(select);

			var childrenData = [];
			
			for(var i=0; i<data.length; i++){
				if(data[i].id == select.value){
					childrenData = (typeof(data[i].children) == 'undefined') ? null : data[i].children;
				}
			}

			if(childrenData != null && childrenData.length > 0){
				appendSelect(childrenData);
			}
		}

	}
	
	//删除选择的SELECT后的所有SELSECT
	var removeSelectAfter = function(seleObj){
		
		var selects = cont.getElementsByTagName("select");
		var length = selects.length;
		var temp = 0;
		
		//先找出选择的是第几个SELECT
		for(var i=0; i<length; i++){
			if(selects[i] == seleObj){	
				temp = i;
			}	
		}

		//删除当前SELECT后的所有SELECT
		for(var i=0; i<(length - temp -1); i++){
			cont.removeChild(selects[temp+1]);
		}

	}

}


/** 调用示例
var nlc = new NoLimitCategory(<?php echo json_encode($treeData, JSON_UNESCAPED_UNICODE)?>, document.getElementById("cont_category"), "全部");

nlc.onSelectCreate = function(selectObj, n){
	if(n == 1){
		selectObj.style.cssText = 'margin-left:0px;height:22px;';
	}else{
		selectObj.style.cssText = 'margin-left:5px;height:22px;';	
	}
};

var cids = [];
if(c1 != 0)cids.push(c1);
if(c2 != 0)cids.push(c2);
if(c3 != 0)cids.push(c3);


nlc.append(cids);




*/